package com.example.opengldemo.simple12_03;

import android.opengl.GLES30;
import android.opengl.GLSurfaceView;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;

import com.example.opengldemo.R;
import com.example.opengldemo.util.MatrixState;
import com.example.opengldemo.util.TextureUtil;

import static com.example.opengldemo.simple12_03.Constant.*;

class MySurfaceView extends GLSurfaceView {
    private SceneRenderer mRenderer;
    int textureFloor;
    int textureFloorBTM;
    int textureBallId;

    public MySurfaceView(Context context) {
        super(context);
        this.setEGLContextClientVersion(3);
        //this.setEGLConfigChooser(8, 8, 8, 8, 16, 8);
        mRenderer = new SceneRenderer();
        setRenderer(mRenderer);
        setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
    }

    private class SceneRenderer implements Renderer {
        TextureRect texRect;
        BallTextureByVertex btbv;
        BallForControl bfd;



        public void onDrawFrame(GL10 gl) {
            //清空深度缓存 和 颜色缓存
            GLES30.glClear(GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
            //保存场景
            MatrixState.pushMatrix();
            //移动
            MatrixState.translate(0, -2, 0);

            //清除模版缓存数据
            GLES30.glClear(GLES30.GL_STENCIL_BUFFER_BIT);

            GLES30.glEnable(GLES30.GL_STENCIL_TEST);

            GLES30.glStencilFunc(GLES30.GL_ALWAYS, 3, 1);

            GLES30.glStencilOp(GLES30.GL_KEEP, GLES30.GL_KEEP, GLES30.GL_REPLACE);

            texRect.drawSelf(textureFloor);


            GLES30.glStencilFunc(GLES30.GL_EQUAL, 4, 3);

            GLES30.glStencilOp(GLES30.GL_KEEP, GLES30.GL_KEEP, GLES30.GL_KEEP);

            bfd.drawSelfMirror(textureBallId);

            GLES30.glDisable(GLES30.GL_STENCIL_TEST);


            //GLES30.glEnable(GLES30.GL_BLEND);

            //GLES30.glBlendFunc(GLES30.GL_SRC_ALPHA, GLES30.GL_ONE_MINUS_SRC_ALPHA);
            //texRect.drawSelf(textureFloorBTM);

            //GLES30.glDisable(GLES30.GL_BLEND);

            //bfd.drawSelf(textureBallId);
            MatrixState.popMatrix();
        }

        public void onSurfaceChanged(GL10 gl, int width, int height) {

            GLES30.glViewport(0, 0, width, height);

            float ratio = (float) width / height;

            MatrixState.setProjectFrustum(-ratio, ratio, -1, 1, 3, 100);

            MatrixState.setCamera(0.0f, 8.0f, 8.0f, 0, 0f, 0, 0, 1, 0);
        }

        public void onSurfaceCreated(GL10 gl, EGLConfig config) {

            GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

            texRect = new TextureRect(MySurfaceView.this, 4, 2.568f);

            btbv = new BallTextureByVertex(MySurfaceView.this, BALL_SCALE);

            bfd = new BallForControl(btbv, 3f);

            GLES30.glDisable(GLES30.GL_DEPTH_TEST);

            textureFloor = TextureUtil.getTextureId(MySurfaceView.this, R.drawable.mdb, null, false);
            textureFloorBTM = TextureUtil.getTextureId(MySurfaceView.this, R.drawable.mdbtm, null, false);
            textureBallId = TextureUtil.getTextureId(MySurfaceView.this, R.drawable.basketball, null, false);


            GLES30.glEnable(GLES30.GL_CULL_FACE);

            MatrixState.setInitStack();
        }
    }
}
